#include <stdio.h>
#include "LinkStack.h"
#include <stdio.h>

bool IsNumber(char c)
{
    if (c >= '0' && c <= '9')
    {
        return true;
    }
    return false;
}

bool IsOper(char c)
{
    switch (c)
    {
    case '+':
    case '-':
    case '*':
    case '/':
    case '=':
    case '(':
    case ')':
        return true;
    default:
        return false;
    }
}

int Operate(int prev, int back, char c)
{
    switch (c)
    {
    case '+': return prev + back;
    case '-': return prev - back;
    case '*': return prev * back;
    case '/': 
        if (back == 0)
        {
            printf("/ 0 error!\n");
            exit(-1);
        }
        return prev / back;
    default:
        break;
    }
}

char Precede(char ch1, char ch2)
{
    char pre[7][7] = 
    {
        // + - * / ( ) =
        { '>', '>', '<', '<', '<', '>', '>'}, // +
        { '>', '>', '<', '<', '<', '>', '>'}, // -
        { '>', '>', '>', '>', '<', '>', '>'}, // *
        { '>', '>', '>', '>', '<', '>', '>'}, // /
        { '<', '<', '<', '<', '<', '=', '0'}, // (
        { '>', '>', '>', '>', '0', '>', '>'}, // )
        { '<', '<', '<', '<', '<', '<', '<'}  // =
    };
    int i, j;
    switch (ch1)
    {
    case '+': i = 0; break;
    case '-': i = 1; break;
    case '*': i = 2; break;
    case '/': i = 3; break;
    case '(': i = 4; break;
    case ')': i = 5; break;
    case '=': i = 6; break;
    default:
        break;
    }
    switch (ch2)
    {
    case '+': j = 0; break;
    case '-': j = 1; break;
    case '*': j = 2; break;
    case '/': j = 3; break;
    case '(': j = 4; break;
    case ')': j = 5; break;
    case '=': j = 6; break;
    default:
        break;
    }
    return pre[i][j];
}

int main()
{
    LStack Nums;
    Init(&Nums);

    LStack Opers;
    Init(&Opers);

    SPush(&Opers, '=');

    char str[100] = {0};
    scanf("%s", str);

    int i = 0;
    while (str[i] != '\0')
    {
        if (IsNumber(str[i]) == true)
        {
            int num = str[i] - '0';
            SPush(&Nums, num);
            i++;
            while (IsNumber(str[i]) == true)
            {
                num = str[i] - '0';
                int higher = *GetTop(&Nums);
                SPop(&Nums);
                SPush(&Nums, higher * 10 + num);
                i++;
            }
        }
        else if (IsOper(str[i]) == true)
        {
            char symbol = Precede(*GetTop(&Opers), str[i]);
            if (symbol == '<')
            {
                SPush(&Opers, str[i]);
                i++;
            }
            else if (symbol == '>')
            {
                int back = *GetTop(&Nums);
                SPop(&Nums);
                int prev = *GetTop(&Nums);
                SPop(&Nums);
                char oper = *GetTop(&Opers);
                SPop(&Opers);
                SPush(&Nums, Operate(prev, back, oper));
            }
            else if (symbol == '=')
            {
                SPop(&Opers);
                i++;
            }
        } 
    }
    printf("answer is %d\n", (int)*GetTop(&Nums));
    return 0;
}